package com.xiaoyao.myframeapp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 获取当前时间
 *
 * @author Administrator
 */
public class Time {
    /**
     * 获取当前时间 "yyyy-MM-dd HH:mm:ss"
     *
     * @return
     */
    public static String getsj() {
        Calendar now = Calendar.getInstance();
        // System.out.println("年: " + now.get(Calendar.YEAR));
        // System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        // System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        // System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        // System.out.println("分: " + now.get(Calendar.MINUTE));
        // System.out.println("秒: " + now.get(Calendar.SECOND));
        // System.out.println("当前时间毫秒数：" + now.getTimeInMillis());
        // System.out.println(now.getTime());

        Date d = new Date();
        // System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        // System.out.println("格式化后的日期：" + dateNowStr);

		/*
         * String str = "2012-1-13 17:26:33"; // 要跟上面sdf定义的格式一样 Date today; try
		 * { today = sdf.parse(str); // System.out.println("字符串转成日期：" + today);
		 * } catch (ParseException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
        return dateNowStr;
    }

    /**
     * 获取当前毫秒
     *
     * @return
     */
    public static String getsjhm() {
        Date date = new Date();
        Long time = date.getTime();
        String a = time.toString();
        return a;
    }

    /**
     * 根据日期获得星期
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六"};
        String[] weekDaysCode = {"0", "1", "2", "3", "4", "5", "6"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
//		return weekDaysCode[intWeek];
        return weekDaysName[intWeek];
    }

    /**
     * 获取当前时间差
     *
     * @param t 毫秒
     * @return -天
     */
    public static int getTimeDifference(String t) {
        Date date = new Date();
        Long time = date.getTime();
        time = Long.valueOf(t) - time;
        time = time / (24 * 60 * 60 * 1000);
        return Integer.valueOf(time + "");
    }

    /**
     * 获取当前时间差
     *
     * @param t 毫秒
     * @return 天
     */
    public static int getTimeDifference_just(String t) {
        Date date = new Date();
        Long time = date.getTime();
        time = time - Long.valueOf(t);
        time = time / (24 * 60 * 60 * 1000);
        return Integer.valueOf(time + "");
    }

    /**
     * 获取当前时间差
     *
     * @param t 毫秒
     * @return Minutes 小时
     */
    public static int getTimeDifference_h(String t) {
        Date date = new Date();
        Long time = date.getTime();
        time = time - Long.valueOf(t);
        time = time / (60 * 60 * 1000);
        return Integer.valueOf(time + "");
    }

    /**
     * 获取当前时间差
     *
     * @param t 毫秒
     * @return Minutes 分钟
     */
    public static int getTimeDifference_m(String t) {
        Date date = new Date();
        Long time = date.getTime();
        time = time - Long.valueOf(t);
        time = time / (60 * 1000);
        return Integer.valueOf(time + "");
    }

    /**
     * 毫秒转换成时间
     *
     * @param
     * @return
     */
    public static Date gethmtorsj(String hm) {
        long l = Long.valueOf(hm);
        Date d = new Date(l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(d));
        return d;
    }

    /**
     * 将时间转换为时间戳
     */
    public static String dateToStampyyyyMMddHHmmss(String s) {
        String res = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");//TODO 不同返回时间类型 需要不同的设置
        Date date;
        try {
            date = simpleDateFormat.parse(s);

            long ts = date.getTime();
            res = String.valueOf(ts);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 将时间转换为时间戳
     */
    public static String dateToStampyyyy_MM_dd(String s) {
        String res = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd");//TODO 不同返回时间类型 需要不同的设置
        Date date;
        try {
            date = simpleDateFormat.parse(s);

            long ts = date.getTime();
            res = String.valueOf(ts);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 将时间戳转换为时间 yyyy年MM月dd日 HH点mm分ss秒
     */
    public static String stampToDateyyyyMMddHHmmss(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 yyyy年MM月dd日 HH:mm
     */
    public static String stampToDateyyyyMMddHHmm(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 yyyy年MM月dd日
     */
    public static String stampToDateyyyyMMdd(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 将时间戳转换为时间 MM月dd日
     */
    public static String stampToDateMMdd(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 MM-dd
     */
    public static String stampToDateMM_dd(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 HH:mm
     */
    public static String stampToDateHHmm(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 yyyy-MM-dd HH:mm:ss
     */
    public static String stampToDateyyyy_MM_dd_HH_mm_ss(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间 yyyy-MM-dd HH:mm:ss
     */
    public static String stampToDateyyyy_MM_dd_HH_mm(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
}
